package com.hbnu.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect  //表明该类的对象为切面类
public class LogRecord {
    //    @Pointcut("bean(CartService)") //粗粒度
    @Pointcut("execution(* com.hbnu.service.CartService.addCart())") // (..)表示任意参数
    public void pointcut() {
    }

    @Before("execution(* com.hbnu.service.CartService.addCart())")
    public void beforeRecord() {
        System.out.println("前置通知：目标方法之前执行...");
    }

    @Around("execution(* com.hbnu.service.CartService.addCart())")
    public void aroundRecord(ProceedingJoinPoint proceedingJoinPoint) {
        System.out.println("环绕通知：目标方法之前执行...");

        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        System.out.println("环绕通知：目标方法之后执行...");
    }
}
